<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>php中几个和mysql相关的扩展模块 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>php中几个和mysql相关的扩展模块</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a> <a class="article__tag-link" href="/tags/Php/">Php</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在编译php的时候，有这么几个和mysql相关的编译选项：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">--with-mysql</div><div class="line">--with-mysqli</div><div class="line">--with-pdo-mysql</div><div class="line">--<span class="built_in">enable</span>-mysqlnd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那这几个选项到底有什么不同呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php官方文档上有这么一段话：</p>
<blockquote>
<p>MySQL:This extension is deprecated as of PHP 5.5.0, and has been removed as of php 7.0.0.<br>MYSQLI: mysql Improved Extension<br>MySQLND: MySQL Native Drive<br>PDO:The PHP Data Objects。extension defines a lightweight, consistent interface for accessing databases in PHP。</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;翻译成中文大概意思是：</p>
<blockquote>
<p>MySQL扩展：在5.5.0版本中开始废弃，7.0.0版本中已经移除（也就是不支持了）<br>MySQLI扩展：是mysql扩展的增强版<br>MySQLnd：这是官方驱动，或者叫做原生驱动<br>PDO：PHP Data Objects，是PHP应用中的一个数据库抽象层规范</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在PHP扩展的角度上看，MYSQL和MYSQLi还是比较上层的扩展，依赖更底层的库去连接和访问数据库。 MYSQLND 就是所说的底层的数据库驱动。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从应用的层面上看，我们通过PHP 的MYSQL或者MYSQLi扩展提供的API去操作数据库。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从底层来看，MYSQLND提供了底层和数据库交互的支持(可以简单理解为和MySQL server进行网络协议交互)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而PDO，则提供了一个统一的API接口，使得你的PHP应用不去关心具体要连接的数据库服务器系统类型。也就是说，如果你使用PDO的API，可以在任何需要的时候无缝切换数据库服务器。比如MYSQL,SQLITE任何数据库都行。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;即从大部分功能上看，PDO提供的API接口和MYSQLI提供的接口对于普通的增删改查效果是一致的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后再贴几个php的代码，来区分几个扩展的用法：</p>
<ol>
<li>mysql连接</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line"><span class="variable">$conn</span> = @ mysql_connect(<span class="string">"localhost"</span>, <span class="string">"root"</span>, <span class="string">""</span>) or die(<span class="string">"数据库连接错误"</span>);</div><div class="line">mysql_select_db(<span class="string">"bbs"</span>, <span class="variable">$conn</span>);</div><div class="line">mysql_query(<span class="string">"set names 'utf8'"</span>);</div><div class="line"><span class="built_in">echo</span> <span class="string">"数据库连接成功"</span>;</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<ol>
<li>mysqli连接</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line"><span class="variable">$conn</span> = mysqli_connect(<span class="string">'localhost'</span>, <span class="string">'root'</span>, <span class="string">''</span>, <span class="string">'bbs'</span>);</div><div class="line"><span class="keyword">if</span>(!<span class="variable">$conn</span>)&#123;</div><div class="line">    die(<span class="string">"数据库连接错误"</span> . mysqli_connect_error());</div><div class="line">&#125;</div><div class="line"><span class="keyword">else</span>&#123;</div><div class="line">    <span class="built_in">echo</span><span class="string">"数据库连接成功"</span>;</div><div class="line">&#125;</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<ol>
<li>PDO连接</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">try&#123;</div><div class="line">    <span class="variable">$pdo</span>=new pdo(<span class="string">"mysql:host=localhost;dbname=bbs"</span>,<span class="string">"root"</span>,<span class="string">""</span>);</div><div class="line">&#125;</div><div class="line">catch(PDDException <span class="variable">$e</span>)&#123;</div><div class="line">    <span class="built_in">echo</span><span class="string">"数据库连接错误"</span>;</div><div class="line">&#125;</div><div class="line"><span class="built_in">echo</span><span class="string">"数据库连接成功"</span>;</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Php/15. php中几个和mysql相关的扩展模块/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
